/**
 * promise重试
 * @param {function(...any): Promise} promiseFunction 一个返回promise的函数
 * @param {number} maxRetries 最大重试次数
 * @param  {...any} args 返回promise的函数的参数
 * @returns {Promise<any>}
 */
export function retryPromiseWithArgs(promiseFunction, maxRetries, ...args) {
    return new Promise((resolve, reject) => {
        function attemptRetry(retriesLeft) {
            promiseFunction(...args)
                .then(resolve)
                .catch(error => {
                    if (retriesLeft > 1) {
                        // console.log(`Retrying... (${retriesLeft} retries left)`);
                        attemptRetry(retriesLeft - 1);
                    } else {
                        reject(new Error('Max retries reached. Promise still rejected. Error is ' + error.message));
                    }
                });
        }

        attemptRetry(maxRetries);
    });
}

